package lzupdater.gui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;

import lzupdater.com.CommunicationHub;
import lzupdater.gui.MainGui;
import lzupdater.gui.listeners.LZFieldListener;
import lzupdater.gui.listeners.ProfileListener;
import lzupdater.locale.LocalizedString;
import lzupdater.log.Logger;
import lzupdater.net.LZUpdater;
import lzupdater.utils.LZULogger;
import lzupdater.utils.LZUPreferences;
import lzupdater.utils.Launcher;
import lzupdater.utils.Profile;
import lzupdater.utils.WoWPathGuesser;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)

//This file is part of LZRosterUpdater.

//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * 
 * @author Fraskor
 */
public class UpdateAction extends AbstractAction implements ProfileListener {

	private final static LocalizedString noProfileSelected = new LocalizedString("No profile selected");
	private final static LocalizedString couldNotUpdateProfile = new LocalizedString("Could not update the profile");
	private final static LocalizedString wowRootPathInvalid = new LocalizedString("Could not launch the World of Warcraft");
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5788087685685425540L;
	private Profile profile;
	private Logger logger;
	private LZFieldListener[] fieldListeners = null;
	private final JButton updateButton;
	private final boolean launchBefore;
	
	private LZUPreferences lzPreferences;
	private LZUpdater lzUpdater;
	private MainGui mainGui;

	public UpdateAction(CommunicationHub hub, JButton updateButton, boolean launchWoWBefore) {
		this.logger = hub.getComponent(LZULogger.class);
		this.lzUpdater = hub.getComponent(LZUpdater.class);
		this.lzPreferences = hub.getComponent(LZUPreferences.class);
		this.mainGui = hub.getComponent(MainGui.class);

		this.updateButton = updateButton;
		this.launchBefore = launchWoWBefore;
		setProfile(null);
	}

	public void setLZFieldListener(LZFieldListener[] fieldListerners) {
		fieldListeners = fieldListerners;
	}

	public void actionPerformed(ActionEvent arg0) {
		logger.clear();
		if (profile == null) {
			logger.error(noProfileSelected);
		} else {
			if (fieldListeners != null) {
				for (LZFieldListener l : fieldListeners) {
					l.updateAsked();
				}
			}

			if (launchBefore) {
				launchWoWAndUpdate();
			} else { 
				updateWithoutLaunchingWoW();
			}
		}
	}

	private void launchWoWAndUpdate() {
		String rootPath = lzPreferences.getWowRootPath();

		Process process = null;
		
		if (lzPreferences.isWowRootPathValide()) {
			try {
				String program = WoWPathGuesser.getInstance().getExecutablePath(rootPath);
				process = Launcher.getInstance().launch(program);
			} catch (Exception e) {
				process = null;
			}
		}

		if (process != null) {
			mainGui.getGui().setEnabled(false);
			try {
				process.waitFor();
			} catch (Exception e) {
				//
			}
			mainGui.getGui().setEnabled(true);
			updateWithoutLaunchingWoW();
		} else {
			logger.error(wowRootPathInvalid);
		}
 	}
	
	
	private void updateWithoutLaunchingWoW() {
		try {
			Thread t = new Thread() {
				@Override
				public void run() {
					try {
						lzUpdater.updateRoster(profile, logger);
					} catch (Exception e) {
						//
					}
				}
			};
			t.start();
		} catch (Exception e) {
			logger.error(couldNotUpdateProfile);
		}		
	}


	public void setProfile(Profile profile) {
		this.profile = profile;
		updateButton.setEnabled(profile != null);
	}

}
